package ads.util;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

public class ClientConfig {
	private static final String SPREAD_CONFIG = "client.properties";

	private Integer regstryPort;
	private String regstryHost;
	private String serverName;
	private Integer clientPort;

	public static ClientConfig load() {
		Properties prop = new Properties();
		try {
			java.net.URL url = ClassLoader.getSystemResource(SPREAD_CONFIG);

			if (url == null) {
				url = Thread.currentThread().getContextClassLoader()
						.getResource(SPREAD_CONFIG);
			}
			try {
				prop.load(url.openStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		ClientConfig config = new ClientConfig();

		config.regstryPort = Integer.valueOf(prop
				.getProperty("rmi.regstry.port"));
		config.regstryHost = prop.getProperty("rmi.regstry.host");
		config.serverName = prop.getProperty("rmi.server.name");
		config.clientPort = Integer
				.valueOf(prop.getProperty("rmi.client.port"));

		return config;
	}

	public static ClientConfig parse(String address, String clientport) {
		try {
			URI uri = new URI(address);
			ClientConfig config = new ClientConfig();
			config.setRegstryHost(uri.getHost());
			config.setRegstryPort(uri.getPort());
			String serverName = uri.getPath();
			if (serverName.charAt(0) == '/') {
				serverName = serverName.substring(1, serverName.length());
			}
			if (serverName.charAt(serverName.length() - 1) == '/') {
				serverName = serverName.substring(0, serverName.length() - 1);
			}
			config.setServerName(serverName);
			config.setClientPort(Integer.valueOf(clientport));
			if (!config.check()) {
				config = null;
			}
			return config;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	

	private boolean check() {
		if (this.getClientPort() < 0 || this.getClientPort() < 0)
			return false;
		if (this.getRegstryHost() == null || this.getRegstryHost().isEmpty()) {
			return false;
		}
		if (this.getServerName() == null || this.getServerName().isEmpty()) {
			return false;
		}

		return true;
	}

	public Integer getRegstryPort() {
		return regstryPort;
	}

	public void setRegstryPort(Integer regstryPort) {
		this.regstryPort = regstryPort;
	}

	public String getRegstryHost() {
		return regstryHost;
	}

	public void setRegstryHost(String regstryHost) {
		this.regstryHost = regstryHost;
	}

	public String getServerName() {
		return serverName;
	}

	public void setServerName(String serverName) {
		this.serverName = serverName;
	}

	public String toString() {
		return "rmi://" + this.getRegstryHost() + ":" + this.getRegstryPort()
				+ "/" + this.getServerName();
	}

	public Integer getClientPort() {
		return clientPort;
	}

	public void setClientPort(Integer clientPort) {
		this.clientPort = clientPort;
	}

}
